package com.example.teamtribe.main.mine.setting.certify;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.ParseMD5;
import com.example.teamtribe.utils.URLStore;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CertifyModel {

    public void getCodeImage(String token, int schoolId, final CertifyBack certifyBack) {
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", token);
        params.put("schoolId", schoolId + "");
        HttpUtil.post(URLStore.USER_CSUST_CODE, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                certifyBack.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String header = response.header("Set-Cookie");
                if (TextUtils.isEmpty(header)) {
                    error();
                    return;
                }
                int index = header.indexOf("=");
                if (index < 0) {
                    error();
                    return;
                }
                String sessionId = header.substring(index + 1);
                if (TextUtils.isEmpty(sessionId)) {
                    error();
                    return;
                }
                Log.d("header", header);
                Log.d("sessionId", sessionId);

                InputStream inputStream = response.body().byteStream();
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = inputStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                }
                inputStream.close();
                byte[] image = outStream.toByteArray();
                outStream.close();

                if (image.length == 0) {
                    error();
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("sessionId", sessionId);
                    bundle.putByteArray("image", image);
                    certifyBack.success(bundle);
                }
            }

            private void error() {
                certifyBack.error("教务网未知错误\n请稍后再试");
            }
        });
    }

    public void sendIdentify(String token, int schoolId, String no, String psw, String code, String sessionId, final CertifyBack certifyBack) {
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", token);
        params.put("schoolId", schoolId + "");
        params.put("no", no);
        params.put("password", ParseMD5.parseStrToMd5U32(psw));
        params.put("validateCode", code);
        params.put("sessionId", sessionId);
        HttpUtil.post(URLStore.USER_CSUST_SEND, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                certifyBack.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    Log.d("csust", object.toString());
                    final int status = object.getInt("code");
                    if (status == 0) {
                        Bundle bundle = new Bundle();
                        bundle.putString("ok", "已发送验证信息");
                        certifyBack.success(bundle);
                    } else if (status == 11 || status == 14) {
                        certifyBack.error("教务网未知错误\n请稍后再试");
                    } else {
                        certifyBack.failure(HttpUtil.getCodeMeg(status));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public interface CertifyBack extends com.example.teamtribe.common.Back {
        void error(String meg);
    }
}
